package com.az.usr.controller;


import com.az.servicecommon.util.BaseResponse;
import com.az.usr.domain.dto.UserMessageDto;
import com.az.miaoapi.vo.TheVo;
import com.az.usr.service.UserMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

@RestController
@RequestMapping("/user")
@BaseResponse
@Slf4j
public class UserMessageController {


    @Autowired
    private UserMessageService userMessageService;

    @GetMapping
    @PreAuthorize("hasAnyAuthority({'loginUser'})")
    public TheVo GetUserMessage(Principal principal) {
        //获取当前登录用户信息
        long userId = Long.parseLong(principal.getName());
        return userMessageService.getMessage(userId);

    }

//    更新/提交用户详细信息
    @PutMapping
    @PreAuthorize("hasAnyAuthority({'loginUser'})")
    public TheVo updateUserMessage(
//            用户信息表必须保证用户名唯一
           @RequestBody UserMessageDto userMessageDto,
            Principal principal)  {
        // 获取当前登录的用户名
        long userId = Long.parseLong(principal.getName());
        return userMessageService.updateUserMessage(userMessageDto, userId);

    }


}